package com.dailyblue.smbms.controller;

import com.dailyblue.smbms.bean.User;
import com.dailyblue.smbms.service.UserService;
import com.dailyblue.smbms.service.impl.UserServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet("/user_update_pass")
public class UserUpdatePassController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("USER_INFO");
        String pass = request.getParameter("pass");
        String oldPass = request.getParameter("oldpassword");
        UserService service = new UserServiceImpl();
        boolean b = service.checkPassIsRight(user.getId(), oldPass);
        if (!b) {
            request.getRequestDispatcher("update_pass.html").forward(request, response);
            return;
        }
        service.updatePass(user.getId(), pass);
        // response.sendRedirect("login.html");
        request.getRequestDispatcher("login.html").forward(request, response);
    }
}